/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectedUsersForm.java
 *
 * Created on Jul 18, 2010, 9:53:14 AM
 */

package forum.client.ui;

import forum.client.controllerlayer.ControllerHandler;
import forum.client.controllerlayer.ControllerHandlerFactory;
import forum.client.ui.events.ForumTreeErrorEvent;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Alon Furman
 */
public class ConnectedUsersForm extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = -5146510193013272322L;
	Component fatherComp;
    private ExecutorService m_pool = Executors.newCachedThreadPool();
    private ControllerHandler m_pipe;
    Component myself;
    String connectedUsers;
    /** Creates new form ConnectedUsersForm */
    public ConnectedUsersForm() {
        initComponents();
    }

    public ConnectedUsersForm(Component c){
        initComponents();
        this.m_pipe = ControllerHandlerFactory.getPipe();
        this.fatherComp = c;
        this.setTitle("Conected Users");
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        SwingWorker<String[], Void> connectedUsersWorker = new SwingWorker<String[],Void>() {

            @Override
            protected String[] doInBackground() {
                return m_pipe.getConnectedUsers();
            }  
        };
        m_pool.execute(connectedUsersWorker);
        String[] ans=null;
        try{
            ans = (String[])connectedUsersWorker.get();
            if (ans[0].equals("succ")){
                String[] parsedConnected = ans[1].split("\n");
                String[] loggedIn = parsedConnected[0].split(", ");
                Vector<String> listEntries = new Vector<String>();
                for (String s : loggedIn){
                    listEntries.add(s);
                }
                for(int i=1; i<parsedConnected.length; i++){
                    listEntries.add(parsedConnected[i]);
                }
                jList1.setListData(listEntries);
            } else{
                m_pipe.notifyObservers(new ForumTreeErrorEvent(ans[1],this));
            }
        }
        catch (InterruptedException ignore) {}
        catch (java.util.concurrent.ExecutionException e) {
             JOptionPane.showMessageDialog(this,
			    "Connected users swing worker failed",
			    "Operation failed.",
			    JOptionPane.WARNING_MESSAGE);
        }
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            	jButton1ActionPerformed(null);
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Connected:");

        jButton1.setText("Back");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(101, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(208, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(203, 203, 203))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
